package com.zyj.exam_app.question_manager.controller;

import com.zyj.exam_app.question_manager.bean.QuestionBean;
import com.zyj.exam_app.question_manager.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/questionManager")
public class QuestionManagerController {

    @Autowired
    private QuestionService questionService;

    /**
     * 请求试题列表界面
     * @return
     */
    @RequestMapping("/questionList")
    public String quertionListPage(Model model){
        //查询所有问题列表
        List<QuestionBean> questionList = questionService.findAllQuestions();

        model.addAttribute("questionList", questionList);
        return "question/list";
    }



    @PostMapping("/addOneQuestion")
    public String addOneQuestion(QuestionBean question){
        questionService.addOneQuestion(question);
        return "forward:/questionManager/questionList";
    }

    @GetMapping("/addPage")
    public String addQuestionPage(){

        return "question/add";
    }

    @GetMapping("/deleteOneQuestion/{questionId}")
    public String deleteOneQuestion(@PathVariable("questionId") String questionId){
        questionService.deleteOneQuestionById(questionId);
        return "forward:/questionManager/questionList";
    }

    /**
     * 随机选题
     * @return
     */
    @GetMapping("/randomSelectQuestions/{num}")
    @ResponseBody
    public Map<String, Object> randomSelectQuestions(@PathVariable("num") int num){
        System.out.println("随机选题 " + num);

        Map<String, Object> result = new HashMap<>();
        result.put("flag", true);
        result.put("questionList", questionService.randomSelectionQuestion(num, "简答题"));    //目前仅抽取简答题
        return result;
    }

}
